package lx.exalarm.activity;

import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.io.IOException;

import lx.exalarm.R;
import lx.exalarm.alarm.ActionBean;
import lx.exalarm.base.BaseSimplePlayActivity;
import lx.exalarm.manager.GlobalThreadManager;
import lx.exalarm.utils.log.Log;
import lx.exalarm.view.VideoPlayView;

/**
 * Created by liuxu on 15-3-3.
 */
public class ActivityPlayVideo extends BaseSimplePlayActivity {

    private RelativeLayout  mPlayViewContainer;
    private VideoPlayView   mPlayView;

    private MediaPlayer     mMediaPlayer;
    private boolean         mAlarmPlayed = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void init(ActionBean action, ActionBean next) {
        mPlayViewContainer = new RelativeLayout(this);
        mPlayView = new VideoPlayView(this);
        RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        p1.addRule(RelativeLayout.CENTER_IN_PARENT);
        mPlayViewContainer.addView(mPlayView, 0, p1);

        RelativeLayout.LayoutParams p2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        p2.addRule(RelativeLayout.CENTER_IN_PARENT);
        getViewContainer().addView(mPlayViewContainer, p2);

        mPlayViewContainer.setBackgroundColor(Color.BLACK);
        mPlayViewContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPlayView.isPlaying()) {
                    mPlayView.pause();
                } else if (mPlayView.isPaused()) {
                    mPlayView.start();
                } else if (mPlayView.isPlaybackComplete()) {
                    mPlayView.start();
                }
            }
        });
    }

    @Override
    protected void startAction(final ActionBean action) {
        if (action == null || action.getData() == null) {
            Log.e("can not play video, data null");
            return;
        }

        if (action.getData().equals(ActionBean.DEFAULT_DATA_VIDEO)) {
            playDefaultAlarm();
        } else if (mAlarmPlayed) {
            playVideo(action);
        } else {
            mAlarmPlayed = true;
            playMusic(action);
        }
    }

    @Override
    protected void stopAction(ActionBean action) {
        stopMusic();
        stopAnim();
        mPlayView.stopPlayback();
    }


    private void playVideo(ActionBean action) {
        mPlayViewContainer.setVisibility(View.VISIBLE);
        Uri uri = Uri.parse(action.getData());
        mPlayView.setVideoURI(uri);
        mPlayView.start();
    }

    // play a short audio first in case the video is soundless
    private void playMusic(final ActionBean action) {
        startAnim();
        mPlayViewContainer.setVisibility(View.GONE);

        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        mMediaPlayer = MediaPlayer.create(this, R.raw.video_pre_alarm_ring);
        mMediaPlayer.setLooping(false);
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mMediaPlayer.release();
                mMediaPlayer = null;

                // play video after delay
                GlobalThreadManager.runInUiThreadDelayed(new Runnable() {
                    @Override
                    public void run() {
                        stopAnim();
                        playVideo(action);
                    }
                }, 2 * 1000);
            }
        });
        mMediaPlayer.start();
    }

    private void stopMusic() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    private void playDefaultAlarm() {
        mPlayViewContainer.setVisibility(View.GONE);
        startAnim();
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        try {
            mMediaPlayer.setDataSource(this, uri);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
        } catch (IOException e) {
            Log.e("can not play default alarm for video, data invalid", e);
            Toast.makeText(this, "fail to play default alarm", Toast.LENGTH_LONG).show();
            return;
        }

        if(!mMediaPlayer.isPlaying()) {
            mMediaPlayer.start();
        }
    }

}
